package app

import (
	"fmt"
	"server/a"
	"server/a0"
	"server/app/sc/dingdan"
	"server/app/sc/shangpinrizhi"
	"server/app/xitong/user"

	"github.com/gin-gonic/gin"
)

func A入口(PrivateGroup *gin.RouterGroup, PublicGroup *gin.RouterGroup, engine *gin.Engine) {
	// sc.A入口(PrivateGroup, PublicGroup, engine)
	PublicGroup.GET("首页头部统计", Aapi.A首页头部统计)
	PublicGroup.GET("订单图表", Aapi.A订单图表)

}

var Aapi = Sapi{}

type Sapi struct {
}

type Sreq struct {
	Cycle string `json:"cycle" form:"cycle" binding:"required"`
}

func (s *Sapi) A订单图表(c *gin.Context) {
	// var req = Sreq{}
	req := c.Query("cycle")
	fmt.Printf("req 👉 %+v\n", req)
	res, _ := dingdan.As7.A图表(req)
	a0.OkWithData(res, c)
}

func (s *Sapi) A首页头部统计(c *gin.Context) {
	// _, _, service := A解构元件(s.A元件)
	// res, _ := service.A成候选值集()
	// res, _ := dingdan.As7.A求和("week")
	res := []S单项{}

	todaySales, _ := dingdan.As7.A求和("today")
	yesterdaySales, _ := dingdan.As7.A求和("yesterday")
	salesTodayRatio := a.Growth(todaySales, yesterdaySales)
	totalSales, _ := dingdan.As7.A求和("month")

	单项 := S单项{
		Today:      todaySales,
		Yesterday:  yesterdaySales,
		TodayRatio: salesTodayRatio,
		Total:      a.A圆整成字符串(totalSales, 2) + "元",
		Date:       "今日",
		Title:      "销售额",
		TotalName:  "本月销售额",
	}
	res = append(res, 单项)

	// // 用户访问量统计
	todayVisits, _ := shangpinrizhi.As7.A计数("today")
	yesterdayVisits, _ := shangpinrizhi.As7.A计数("yesterday")
	visitsTodayRatio := a.Growth(todayVisits, yesterdayVisits)
	totalVisits, _ := shangpinrizhi.As7.A计数("month")
	单项 = S单项{
		Title:      "用户访问量",
		TotalName:  "本月访问量",
		Date:       "今日",
		Today:      todayVisits,
		Yesterday:  yesterdayVisits,
		TodayRatio: visitsTodayRatio,
		Total:      fmt.Sprintf("%dPv", totalVisits),
	}
	res = append(res, 单项)

	// 订单量统计
	today, _ := dingdan.As7.A计数("today")
	yesterday, _ := dingdan.As7.A计数("yesterday")
	ratio := a.Growth(today, yesterday)
	total, _ := dingdan.As7.A计数("month")

	单项 = S单项{
		Today:      today,
		Yesterday:  yesterday,
		TodayRatio: ratio,
		Total:      fmt.Sprintf("%d单", total),
		Date:       "今日",
		Title:      "订单量",
		TotalName:  "本月订单量",
	}
	res = append(res, 单项)

	// // 用户统计
	today, _ = user.As7.A计数("today")
	yesterday, _ = user.As7.A计数("yesterday")
	ratio = a.Growth(today, yesterday)
	total, _ = user.As7.A计数("month")

	单项 = S单项{
		Today:      today,
		Yesterday:  yesterday,
		TodayRatio: ratio,
		Total:      fmt.Sprintf("%d人", total),
		Date:       "今日",
		Title:      "新增用户",
		TotalName:  "本月新增用户",
	}
	res = append(res, 单项)

	// todayUser := h.userService.TodayAddVisits("today", 1)
	// yesterdayUser := h.userService.TodayAddVisits("yesterday", 1)
	// userTodayRatio := a.Growth(float64(todayUser), float64(yesterdayUser))
	// totalUser := h.userService.A计数(map[string]interface{}{
	// 	"time": "month",
	// })
	// user := S单项{
	// 	Title:      "新增用户",
	// 	TotalName:  "本月新增用户",
	// 	Date:       "今日",
	// 	Today:      todayUser,
	// 	Yesterday:  yesterdayUser,
	// 	TodayRatio: userTodayRatio,
	// 	Total:      strconv.Itoa(totalUser) + "人",
	// }

	// 组装结果并设置标题
	// res := []S单项{sales}
	// res = append(res, visits)
	// res = append(res, order)
	// res := []S单项{sales, visits, order, user}

	a0.OkWithData(res, c)
}

// 统计数据结构
type S单项 struct {
	Today      any    `json:"today"`
	Yesterday  any    `json:"yesterday"`
	TodayRatio string `json:"today_ratio"`
	Total      string `json:"total"`
	Date       string `json:"date"`
	Title      string `json:"title"`
	TotalName  string `json:"total_name"`
}
